{block name="main"}
<!-- page -->
<style>
    .form_time{margin-left:15px !important;}
    .red_font{color:#ff0000}
</style>
<form class="form-horizontal pt-15 form-validate">
    <div class="screen-title">
        <span class="text">规则设置</span>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>活动标签</label>
        <div class="col-md-2">
            <input type="text" name="bargain_name" id="bargain_name" value="{$bargain_info['bargain_name']}" required maxlength="4" class="form-control">
        </div>
    </div>

    <div class="form-group ">
        <label class="col-md-2 control-label"><span class="red_font">*</span>活动时间</label>
        <div class="date-input-control form_time">
            <input type="text" class="form-control" name="start_bargain_time" required id="start_bargain_time"  placeholder="开始时间" value="{if $bargain_info['start_bargain_date'] != '1970-01-01'}{$bargain_info['start_bargain_date']}{/if}" autocomplete="off"><i class="icon icon-calendar"></i>
        </div>
        <span class="date-input-group-addon">~</span>
        <div class="date-input-control">
            <input type="text" class="form-control" required id="end_bargain_time" name="end_bargain_time"  placeholder="结束时间" value="{if $bargain_info['end_bargain_date'] != '1970-01-01'}{$bargain_info['end_bargain_date']}{/if}" autocomplete="off"><i class="icon icon-calendar"></i>
        </div>
    </div>

    <div class="form-group" id="sele_goods">
        <label class="col-md-2 control-label"><span class="red_font">*</span>选择商品</label>
        <div class="col-md-3">
            <div  id="selectGoods" class="btn btn-primary-diy">点击选择</div>
        </div><span class="info">开启团队分红后默认所有商品参加</span>
    </div>

    <div id="goods_info_box" class="form-group hidden">
        <label class="col-md-2 control-label"></label>
        <div class="col-md-3">
            <div class="media text-left">
                <div class="media-left goods-img">
                    <img  src="{$bargain_info['pic_cover']}" onerror="this.src='http://iph.href.lu/60x60';" width="60" height="60">
                </div>
                <div class="media-body max-w-300">
                    <div class="line-2-ellipsis goods-text">{$bargain_info['goods_name']}</div>
                    <div class="line-1-ellipsis text-danger strong goods-price">{$bargain_info['start_money']}</div>
                </div>
            </div>
            <input type="hidden" name="goods_id" id="goods_id" value="{$bargain_info['goods_id']}">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>初始金额</label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="start_money" id="start_money" value="{$bargain_info['start_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>最低砍至金额</label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="lowest_money" id="lowest_money" value="{$bargain_info['lowest_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>第一刀砍价</label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="first_bargain_money" id="first_bargain_money" value="{$bargain_info['first_bargain_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div><span class="info">设置后第一刀默认为该价格</span>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>单次砍价金额</label>
        <div class="col-md-4">
            <label class="radio-inline">
                <input type="radio" name="bargain_method" checked value="1" {if $bargain_info['bargain_method']==1}checked{/if}> 固定金额
            </label>
            <label class="radio-inline">
                <input type="radio" name="bargain_method" value="2" {if $bargain_info['bargain_method']==2}checked{/if}> 随机金额
            </label>
        </div>
    </div>
    <div class="form-group fix_bargain_money">
        <label class="col-md-2 control-label"></label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="fix_money" min="0" step="0.01" id="fix_money" value="{$bargain_info['fix_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div>
    </div>

    <div class="form-group rand_bargain_money" style="display:none;">
        <label class="col-md-2 control-label"></label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" placeholder="最低" min="0" step="0.01" name="rand_lowest_money" id="rand_lowest_money" value="{$bargain_info['rand_lowest_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div>
        <div class="col-md-0">~</div>
        <div class="col-md-2" style="margin-left: 5px;
                                     margin-top: -20px;">
            <div class="input-group">
                <input class="form-control" type="number" placeholder="最高" min="0" step="0.01" name="rand_highest_money" id="rand_highest_money" value="{$bargain_info['rand_highest_money']}" required>
                <div class="input-group-addon">元</div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label">活动库存</label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="bargain_num" id="bargain_num" value="{$bargain_info['bargain_stock']}">
                <div class="input-group-addon">件</div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label">限购</label>
        <div class="col-md-2">
            <div class="input-group">
                <input class="form-control" type="number" name="limit_buy" id="limit_buy" value="{$bargain_info['limit_buy']}">
                <div class="input-group-addon">件</div>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label"><span class="red_font">*</span>自己是否参与砍价</label>
        <div class="col-md-4">
            <label class="radio-inline">
                <input type="radio" required name="is_my_bargain" {if $bargain_info['is_my_bargain']==1}checked{/if} value="1"> 是
            </label>
            <label class="radio-inline">
                <input type="radio" required {if $bargain_info['is_my_bargain']==0}checked{/if} name="is_my_bargain" value="0"> 否
            </label>
        </div>
    </div>

    <div id="sub_val">
        <div class="form-group">
            <label class="col-md-2 control-label"></label>
            <div class="col-md-8">
                <button class="btn btn-primary-diy" type="submit" id="submit">添加</button>
                <a href="javascript:history.go(-1);" class="btn btn-default-diy">返回</a>
            </div>
        </div>
    </div>

</form>

<!-- page end -->
{/block}
{block name="script"}
<script>
    require(['util'], function (util) {
        util.layDate("#start_bargain_time");
        util.layDate("#end_bargain_time");
        if("{$bargain_id}" != '0'){
            $('#goods_info_box').removeClass('hidden');
            $('#goods_info_box').addClass('show');
        }
        if($('input[name=bargain_method]:checked').val() == '1'){
            $('.fix_bargain_money').show();
            $('.rand_bargain_money').hide();
            $('#rand_lowest_money').attr('required',false);
            $('#rand_highest_money').attr('required',false);
            $('#start_bargain_time').attr('required',false);
            $('#end_bargain_time').attr('required',false);
        }else{
            $('.fix_bargain_money').hide();
            $('.rand_bargain_money').show();
            $('#fix_money').attr('required',false);
            $('#start_bargain_time').attr('required',false);
            $('#end_bargain_time').attr('required',false);
        }
        //点击固定金额和随机金额切换表单
        $('input[name=bargain_method]').change(function(){
            if($(this).val() == 1){
                $('.fix_bargain_money').show();
                $('.rand_bargain_money').hide();
                $('#rand_lowest_money').attr('required',false);
                $('#rand_highest_money').attr('required',false);
            }else{
                $('.fix_bargain_money').hide();
                $('.rand_bargain_money').show();
                $('#fix_money').attr('required',false);
            }
        })


        //添加
        util.validate($('.form-validate'), function (form) {
            //判断商品是否选择了
            var goods_id = $('#goods_id').val();
            if(goods_id == ''){
                $('#sele_goods').addClass('has-error');
                var info = '<span id="group-error" class="help-block-error">请选择商品</span>';
                $('#selectGoods').after(info);
                return;
            }
            var data = {};
            var bargain_name = $('#bargain_name').val();    //标签
            var start_bargain_time = $('#start_bargain_time').val();  //开始时间
            var end_bargain_time = $('#end_bargain_time').val();  //结束时间
            var goods_id = $('#goods_id').val();  //选择商品
            var start_money = $('#start_money').val();  //开始金额
            var lowest_money = $('#lowest_money').val();  //砍至最低金额
            var first_bargain_money = $('#first_bargain_money').val();  //第一刀
            var send_goods_time = $('#send_goods_time').val();  //发货时间
            var bargain_method = $('input[name="bargain_method"]:checked').val();  //活动状态
            var fix_money = $('#fix_money').val();  //固定金额
            var rand_lowest_money = $('#rand_lowest_money').val();  //随机最低金额
            var rand_highest_money = $('#rand_highest_money').val();  //随机最高金额
            var bargain_num = $('#bargain_num').val();  //库存
            var limit_buy = $('#limit_buy').val();  //限购
            var is_my_bargain = $('input[name=is_my_bargain]:checked').val();//我是否可以砍价
            var bargain_id = '{$bargain_id}';
            data.bargain_name = bargain_name;
            data.start_bargain_time = start_bargain_time;
            data.end_bargain_time = end_bargain_time;
            data.goods_id = goods_id;
            data.start_money = start_money;
            data.lowest_money = lowest_money;
            data.first_bargain_money = first_bargain_money;
            data.send_goods_time = send_goods_time;
            data.bargain_method = bargain_method;
            data.fix_money = fix_money;
            data.rand_lowest_money = rand_lowest_money;
            data.rand_highest_money = rand_highest_money;
            data.bargain_stock = bargain_num;
            data.limit_buy = limit_buy;
            data.is_my_bargain = is_my_bargain;
            data.bargain_id = bargain_id;
            $.post('{$addBargain}',
                data,
                function(res){
                    if (res["code"] > 0) {
                        util.message('添加/修改成功', 'success', "{:__URL('admin/Menu/addonmenu?addons=bargainList')}");
                        // util.message('添加成功', 'success', "");
                    } else {
                        util.message(res["message"], 'danger');
                    }
                });
        })

        //选择商品
        $('#selectGoods').click(function () {
            util.goodsDialog("url:{$bargainDialogGoodsList}",function(data){
            });
        });
        // 批量设置
        $('#stock_table').on('click','.batchSet',function(){
            var batch_text = $(this).text();
            var batch_type = $(this).data('batch_type');
            var html = '<form class="form-horizontal padding-15">';
            html += '<div class="form-group"><label class="col-md-3 control-label">'+batch_text+'</label><div class="col-md-8">';
            html += '<input type="number" min="0" oninput="if(value.length>9)value=value.slice(0,9)" name="batch_'+batch_type+'" class="form-control">';
            html += '</div></div></form>';
            util.confirm('批量修改'+batch_text,html,function(){
                var val;
                var maxNum = 9999999.99;
                var currInput = $('#stock_table input[name="'+batch_type+'"]');
                val = this.$content.find('input[name="batch_'+batch_type+'"]').val();
                if(!val || val == ''){
                    util.message(batch_text+'不能为空');
                    return false;
                }else if(val > maxNum && batch_type !== 'goods_code'){
                    util.message('价格最大为 '+maxNum);
                    return false;
                }
                currInput.each(function(i,e){
                    e.value = val;
                });
            });

        });

    });
</script>
{/block}
